package com.pango.core.easyui.support;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.pango.core.easyui.vo.ComboTree;
import com.pango.core.easyui.vo.ComboTreeModel;
import com.pango.core.easyui.vo.TreeGrid;
import com.pango.core.easyui.vo.TreeGridModel;
import com.pango.core.utils.ReflectHelper;
import com.pango.core.utils.StringUtils;

/**
 * 创建页面UI辅助类
 * @author thierry.fu
 *
 */
public class UISupportUtils {
	/**
	 * 根据模型生成ComboTree JSON
	 * @param all全部对象
	 * @param in已拥有的对象
	 * @param comboTreeModel模型
	 * @return
	 */
	public static List<ComboTree> ComboTree(List all, ComboTreeModel comboTreeModel, List in) {
		List<ComboTree> trees = new ArrayList<ComboTree>();
		for (Object obj : all) {
			trees.add(comboTree(obj, comboTreeModel, in, false));
		}
		return trees;

	}

	// 构建ComboTree
	private static ComboTree comboTree(Object obj, ComboTreeModel comboTreeModel, List in, boolean recursive) {
		ComboTree tree = new ComboTree();
		Map<String, Object> attributes = new HashMap<String, Object>();
		ReflectHelper reflectHelper = new ReflectHelper(obj);
		String id = StringUtils.getString(reflectHelper.getMethodValue(comboTreeModel.getIdField()));
		tree.setId(id);
		tree.setText(StringUtils.getString(reflectHelper.getMethodValue(comboTreeModel.getTextField())));
		if (comboTreeModel.getSrcField() != null) {
			attributes.put("href", StringUtils.getString(reflectHelper.getMethodValue(comboTreeModel.getSrcField())));
			tree.setAttributes(attributes);
		}
		if (in == null) {
		} else {
			if (in.size() > 0) {
				for (Object inobj : in) {
					ReflectHelper reflectHelper2 = new ReflectHelper(inobj);
					String inId = StringUtils.getString(reflectHelper2.getMethodValue(comboTreeModel.getIdField()));
					//判断是否选中
					if (inId.equals(id)) {
						tree.setChecked(true);
					}
				}
			}
		}
		List childList = (List) reflectHelper.getMethodValue(comboTreeModel.getChildField());
		if(childList != null && childList.size() > 0) {
				tree.setState("closed");
				tree.setChecked(false);
		}
		return tree;
	}
	
	/**
	 * 构建树形数据表
	 */
	public static List<TreeGrid> treegrid(List all, TreeGridModel treeGridModel) {
		List<TreeGrid> treegrid = new ArrayList<TreeGrid>();
		for (Object obj : all) {
			ReflectHelper reflectHelper = new ReflectHelper(obj);
			TreeGrid tg = new TreeGrid();
			String id = StringUtils.getString(reflectHelper.getMethodValue(treeGridModel.getIdField()));
			String src = StringUtils.getString(reflectHelper.getMethodValue(treeGridModel.getSrc()));
			String text = StringUtils.getString(reflectHelper.getMethodValue(treeGridModel.getTextField()));
			if(StringUtils.isNotEmpty(treeGridModel.getOrder())){
				String order = StringUtils.getString(reflectHelper.getMethodValue(treeGridModel.getOrder()));
				tg.setOrder(order);
			}
			tg.setId(id);
			
			tg.setSrc(src);
			tg.setText(text);
			if (treeGridModel.getParentId() != null) {
				Object pid = reflectHelper.getMethodValue(treeGridModel.getParentId());
				if (pid != null) {
					tg.setParentId(pid.toString());
				} else {
					tg.setParentId("");
				}
			}
			if (treeGridModel.getParentText() != null) {
				Object ptext = reflectHelper.getMethodValue(treeGridModel.getTextField());
				if (ptext != null) {
					tg.setParentText(ptext.toString());
				} else {
					tg.setParentText("");
				}

			}
			List childList = (List) reflectHelper.getMethodValue(treeGridModel.getChildList());

			if (childList != null && childList.size() > 0 ) {
					tg.setState("closed");
			}

			treegrid.add(tg);
		}
		return treegrid;
	}
}
